package o;

import android.util.SparseArray;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.Extractor;
import java.io.IOException;
import o.aea.e;

public final class agm implements Extractor {
    public static final adw ˋ = agp.ॱ;
    private boolean ʻ;
    private boolean ʼ;
    private long ʽ;
    private final agn ˊ;
    private final anq ˎ;
    private final SparseArray<a> ˏ;
    private boolean ˏॱ;
    private ady ͺ;
    private final anc ॱ;
    private boolean ॱॱ;
    private agh ᐝ;

    static final class a {
        private boolean ʻ;
        private boolean ˊ;
        private final agc ˋ;
        private final anq ˎ;
        private final ane ˏ = new ane(new byte[64]);
        private boolean ॱ;
        private long ॱॱ;
        private int ᐝ;

        public a(agc o_agc, anq o_anq) {
            this.ˋ = o_agc;
            this.ˎ = o_anq;
        }

        public void ˊ() {
            this.ʻ = false;
            this.ˋ.ˏ();
        }

        public void ˎ(anc o_anc) throws ParserException {
            o_anc.ˎ(this.ˏ.ˎ, 0, 3);
            this.ˏ.ˋ(0);
            ॱ();
            o_anc.ˎ(this.ˏ.ˎ, 0, this.ᐝ);
            this.ˏ.ˋ(0);
            ˎ();
            this.ˋ.ˋ(this.ॱॱ, 4);
            this.ˋ.ˊ(o_anc);
            this.ˋ.ˋ();
        }

        private void ॱ() {
            this.ˏ.ˎ(8);
            this.ˊ = this.ˏ.ˊ();
            this.ॱ = this.ˏ.ˊ();
            this.ˏ.ˎ(6);
            this.ᐝ = this.ˏ.ॱ(8);
        }

        private void ˎ() {
            this.ॱॱ = 0;
            if (this.ˊ) {
                this.ˏ.ˎ(4);
                long ॱ = ((long) this.ˏ.ॱ(3)) << 30;
                this.ˏ.ˎ(1);
                ॱ |= (long) (this.ˏ.ॱ(15) << 15);
                this.ˏ.ˎ(1);
                ॱ |= (long) this.ˏ.ॱ(15);
                this.ˏ.ˎ(1);
                if (!this.ʻ && this.ॱ) {
                    this.ˏ.ˎ(4);
                    long ॱ2 = ((long) this.ˏ.ॱ(3)) << 30;
                    this.ˏ.ˎ(1);
                    ॱ2 |= (long) (this.ˏ.ॱ(15) << 15);
                    this.ˏ.ˎ(1);
                    ॱ2 |= (long) this.ˏ.ॱ(15);
                    this.ˏ.ˎ(1);
                    this.ˎ.ˏ(ॱ2);
                    this.ʻ = true;
                }
                this.ॱॱ = this.ˎ.ˏ(ॱ);
            }
        }
    }

    public agm() {
        this(new anq(0));
    }

    public agm(anq o_anq) {
        this.ˎ = o_anq;
        this.ॱ = new anc(4096);
        this.ˏ = new SparseArray();
        this.ˊ = new agn();
    }

    public boolean ˎ(adv o_adv) throws IOException, InterruptedException {
        boolean z = true;
        byte[] bArr = new byte[14];
        o_adv.ˎ(bArr, 0, 14);
        if (442 != (((((bArr[0] & 255) << 24) | ((bArr[1] & 255) << 16)) | ((bArr[2] & 255) << 8)) | (bArr[3] & 255)) || (bArr[4] & 196) != 68 || (bArr[6] & 4) != 4 || (bArr[8] & 4) != 4 || (bArr[9] & 1) != 1 || (bArr[12] & 3) != 3) {
            return false;
        }
        o_adv.ˊ(bArr[13] & 7);
        o_adv.ˎ(bArr, 0, 3);
        if (1 != ((bArr[2] & 255) | (((bArr[0] & 255) << 16) | ((bArr[1] & 255) << 8)))) {
            z = false;
        }
        return z;
    }

    public void ˏ(ady o_ady) {
        this.ͺ = o_ady;
    }

    public void ˊ(long j, long j2) {
        if (!((this.ˎ.ˎ() == -9223372036854775807L ? 1 : null) == null && (this.ˎ.ˏ() == 0 || this.ˎ.ˏ() == j2))) {
            this.ˎ.ॱ();
            this.ˎ.ॱ(j2);
        }
        if (this.ᐝ != null) {
            this.ᐝ.ˏ(j2);
        }
        for (int i = 0; i < this.ˏ.size(); i++) {
            ((a) this.ˏ.valueAt(i)).ˊ();
        }
    }

    public void ॱ() {
    }

    public int ˎ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        long ॱ = o_adv.ॱ();
        if ((ॱ != -1) && !this.ˊ.ˏ()) {
            return this.ˊ.ˎ(o_adv, o_adz);
        }
        ॱ(ॱ);
        if (this.ᐝ != null && this.ᐝ.ॱ()) {
            return this.ᐝ.ˋ(o_adv, o_adz, null);
        }
        o_adv.ˏ();
        long ˎ = ॱ != -1 ? ॱ - o_adv.ˎ() : -1;
        if ((ˎ != -1 && ˎ < 4) || !o_adv.ˎ(this.ॱ.ˏ, 0, 4, true)) {
            return -1;
        }
        this.ॱ.ˋ(0);
        int ॱᐝ = this.ॱ.ॱᐝ();
        if (ॱᐝ == 441) {
            return -1;
        }
        if (ॱᐝ == 442) {
            o_adv.ˎ(this.ॱ.ˏ, 0, 10);
            this.ॱ.ˋ(9);
            o_adv.ॱ((this.ॱ.ʽ() & 7) + 14);
            return 0;
        } else if (ॱᐝ == 443) {
            o_adv.ˎ(this.ॱ.ˏ, 0, 2);
            this.ॱ.ˋ(0);
            o_adv.ॱ(this.ॱ.ॱॱ() + 6);
            return 0;
        } else if (((ॱᐝ & -256) >> 8) != 1) {
            o_adv.ॱ(1);
            return 0;
        } else {
            int i = ॱᐝ & 255;
            a aVar = (a) this.ˏ.get(i);
            if (!this.ʻ) {
                if (aVar == null) {
                    agc o_agc = null;
                    if (i == 189) {
                        o_agc = new aga();
                        this.ʼ = true;
                        this.ʽ = o_adv.ˊ();
                    } else if ((i & 224) == 192) {
                        o_agc = new agi();
                        this.ʼ = true;
                        this.ʽ = o_adv.ˊ();
                    } else if ((i & 240) == 224) {
                        o_agc = new agd();
                        this.ॱॱ = true;
                        this.ʽ = o_adv.ˊ();
                    }
                    if (o_agc != null) {
                        o_agc.ˏ(this.ͺ, new com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a(i, 256));
                        aVar = new a(o_agc, this.ˎ);
                        this.ˏ.put(i, aVar);
                    }
                }
                ˎ = (this.ʼ && this.ॱॱ) ? this.ʽ + 8192 : 1048576;
                if (o_adv.ˊ() > ˎ) {
                    this.ʻ = true;
                    this.ͺ.ˊ();
                }
            }
            o_adv.ˎ(this.ॱ.ˏ, 0, 2);
            this.ॱ.ˋ(0);
            ॱᐝ = this.ॱ.ॱॱ() + 6;
            if (aVar == null) {
                o_adv.ॱ(ॱᐝ);
            } else {
                this.ॱ.ˏ(ॱᐝ);
                o_adv.ˏ(this.ॱ.ˏ, 0, ॱᐝ);
                this.ॱ.ˋ(6);
                aVar.ˎ(this.ॱ);
                this.ॱ.ॱ(this.ॱ.ˎ());
            }
            return 0;
        }
    }

    private void ॱ(long j) {
        if (!this.ˏॱ) {
            this.ˏॱ = true;
            if (this.ˊ.ॱ() != -9223372036854775807L) {
                this.ᐝ = new agh(this.ˊ.ˎ(), this.ˊ.ॱ(), j);
                this.ͺ.ˎ(this.ᐝ.ˋ());
                return;
            }
            this.ͺ.ˎ(new e(this.ˊ.ॱ()));
        }
    }
}
